package com.supcalls;

import java.util.function.*;

@FunctionalInterface
interface MyFunctionalInterface {
    void sayMessage(String message);
}

public class FunctionalInterfaceDemo {
    public static void main(String[] args) {
        // 1. 使用自定义函数式接口
        MyFunctionalInterface myFunc = message -> System.out.println("Hello, " + message);
        myFunc.sayMessage("Java 8!");

        // 2. 使用 Java 8 内置的函数式接口

        // Predicate<T> - 判断是否为偶数
        Predicate<Integer> isEven = num -> num % 2 == 0;
        System.out.println("10 是否是偶数: " + isEven.test(10)); // true
        System.out.println("15 是否是偶数: " + isEven.test(15)); // false

        // Function<T, R> - 将字符串转换为整数
        Function<String, Integer> stringToInt = Integer::parseInt;
        System.out.println("String '100' 转换为整数: " + stringToInt.apply("100"));

        // Consumer<T> - 打印消息
        Consumer<String> printMessage = msg -> System.out.println("消费消息: " + msg);
        printMessage.accept("Java 8 的 Consumer 示例");

        // Supplier<T> - 提供一个随机数
        Supplier<Double> randomSupplier = Math::random;
        System.out.println("生成的随机数: " + randomSupplier.get());

        // BiFunction<T, U, R> - 计算两个数的和
        BiFunction<Integer, Integer, Integer> sum = (a, b) -> a + b;
        System.out.println("10 + 20 = " + sum.apply(10, 20));

        // UnaryOperator<T> - 字符串转换为大写
        UnaryOperator<String> toUpperCase = String::toUpperCase;
        System.out.println("转换为大写: " + toUpperCase.apply("hello java"));

        // BinaryOperator<T> - 两个数相乘
        BinaryOperator<Integer> multiply = (a, b) -> a * b;
        System.out.println("5 * 6 = " + multiply.apply(5, 6));
    }
}
